import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * 动态配置行
 * @date 2019-07-15
 */
import { createPagination, getResponse } from 'utils/utils';
import { queryUnifyIdpValue } from 'cpaas-front/lib/services/api';
import { fetchLineList as _fetchLineList, fetchLineById as _fetchLineById, createLine as _createLine, updateLine as _updateLine, removeLine as _removeLine } from "../../services/dynamicForm/lineService";
export default {
  namespace: 'line',
  state: {
    list: [],
    keyTypeList: [],
    pagination: {},
    lineDetail: {}
  },
  effects: {
    // 查询字段配置类型
    fetchKeyTypeList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchKeyTypeList(_, _ref) {
      var call, put, res, keyTypeList;
      return _regeneratorRuntime.wrap(function fetchKeyTypeList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryUnifyIdpValue, 'CPFM.ITEM_TYPE');

            case 3:
              res = _context.sent;
              keyTypeList = getResponse(res);

              if (!keyTypeList) {
                _context.next = 8;
                break;
              }

              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  keyTypeList: keyTypeList
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, fetchKeyTypeList);
    }),
    // 查询列表
    fetchLineList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLineList(_ref2, _ref3) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchLineList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_fetchLineList, payload);

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  pagination: createPagination(list),
                  list: list.content
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchLineList);
    }),
    // 通过 ID 查询明细
    fetchLineById: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLineById(_ref4, _ref5) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchLineById$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call, put = _ref5.put;
              _context3.next = 4;
              return call(_fetchLineById, payload);

            case 4:
              res = _context3.sent;
              list = getResponse(res);

              if (!list) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  lineDetail: list
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchLineById);
    }),
    // 创建
    createLine: /*#__PURE__*/_regeneratorRuntime.mark(function createLine(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createLine$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_createLine, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, createLine);
    }),
    // 更新
    updateLine: /*#__PURE__*/_regeneratorRuntime.mark(function updateLine(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateLine$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_updateLine, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, updateLine);
    }),
    // 删除
    removeLine: /*#__PURE__*/_regeneratorRuntime.mark(function removeLine(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function removeLine$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(_removeLine, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, removeLine);
    }) // 查询所有
    // *fetchAllLine({ payload }, { call }) {
    //   const res = yield call(fetchAllLine, payload);
    //   return getResponse(res);
    // },
    // // 通过Code查询所有
    // *fetchAllLineByCode({ payload }, { call }) {
    //   const res = yield call(fetchAllLineByCode, payload);
    //   return getResponse(res);
    // },

  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};